package com.team.easybuy.controller;

import com.github.pagehelper.PageInfo;
import com.team.easybuy.entity.*;
import com.team.easybuy.service.*;
import com.team.easybuy.util.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/index")
public class IndexController {

    @Resource
    private ProductCategoryService productCategoryService;
    @Resource
    private ProductService productService;
    @Resource
    private NewsService newsService;
    @Resource
    private UserService userService;

    @RequestMapping("/loadNavOne")
    public List<ProductCategory> loadNavOne() {
        return productCategoryService.listProCategory();
    }

    @RequestMapping("/loadNavTwo/{id}")
    public List<ProductCategory2> loadNavTwo(@PathVariable Integer id) {
        return productCategoryService.listProCategory2(id);
    }

    @RequestMapping("/loadNews")
    public List<News> loadNews() {
        return newsService.ListNews();
    }

    @RequestMapping("/loadGoods")
    public List<ProductCategory> loadGoods() {
        return productCategoryService.listGoods();
    }

    @RequestMapping("/login")
    public boolean login(@RequestBody User user, HttpServletRequest req, HttpServletResponse resp) {
        String md5Hex = SecurityUtils.md5Hex(user.getPassword());
        user.setPassword(md5Hex);
        User login = userService.checkLogin(user, req, resp);
        return login != null;
    }

    @RequestMapping("/logout")
    public void logout(HttpServletRequest req, HttpServletResponse resp) {
        userService.loginOut(req, resp);
    }

    @RequestMapping("/loadGoodOne/{id}")
    public Product loadGoodOne(@PathVariable Integer id) {
        return productService.selectById(id);
    }

    @RequestMapping("/searchById")
    public PageInfo<Product> searchById(@RequestParam Map<String, Object> params) {
        return productService.solrById(params);
    }

    @RequestMapping("/searchByName")
    public PageInfo<Product> searchByName(@RequestParam Map<String, Object> params) {
        return productService.solrByName(params);
    }

    @RequestMapping("/getUser")
    public User getUser(HttpServletRequest req, HttpServletResponse resp) {
        return userService.getUser(req, resp);
    }

    @PostMapping("/regist")
    public String regist(@RequestBody User user) {
        String rs = null;
        user.setPassword(SecurityUtils.md5Hex(user.getPassword()));
        if (userService.addUser(user)) {
            rs = "yes";
        } else {
            rs = "no";
        }
        return rs;
    }

    @RequestMapping("/hasLoginName")
    public String hasLoginName(@RequestParam Map<String, Object> map) {
        String rs = null;
        if (userService.containsByParams(map)) {
            rs = "yes";
        } else {
            rs = "no";
        }
        return rs;
    }

    @RequestMapping("/findpwdByEmail/{email}")
    public String sendEmailCode(@PathVariable String email) {
        String rs = null;
        if (userService.sendEmailCode(email)) {
            rs = "yes";
        } else {
            rs = "no";
        }
        return rs;
    }


    @RequestMapping("/findpwdcheckEmailCode/{email}/{emailcode}")
    public String checkEmailCode(@PathVariable("email") String email, @PathVariable("emailcode") String emailcode) {
        String rs = null;
        Boolean flag = userService.checkEmailCode(email, emailcode);
        if (flag == null) {
            rs = "failure";
        } else if (flag) {
            rs = "yes";
        } else {
            rs = "no";
        }
        return rs;
    }

    @RequestMapping("/findpwdcheckMobileCode/{mobile}/{mobilecode}")
    public String checkMobileCode(@PathVariable("mobile") String mobile, @PathVariable("mobilecode") String mobilecode) {
        String rs = null;
        Boolean flag = userService.checkEmailCode(mobile, mobilecode);
        if (flag == null) {
            rs = "failure";
        } else if (flag) {
            rs = "yes";
        } else {
            rs = "no";
        }
        return rs;
    }

    @RequestMapping("/getUserByEmail/{email}")
    public Integer getUseerByEmail(@PathVariable("email") String email) {
        return userService.getUserByEmail(email).getId();
    }

    @RequestMapping("/getUserByMobile/{mobile}")
    public User getUserByMobile(@PathVariable("mobile") String mobile) {
        return userService.getUserByMobile(mobile);
    }

    @PostMapping("/resetPwd")
    public String resetPwd(@RequestBody User user) {
        String rs = null;
        if (userService.resetPwd(user)) {
            rs = "yes";
        } else {
            rs = "no";
        }
        return rs;
    }

    @RequestMapping("/hasEmail")
    public String hasEmail(@RequestParam String email) {
        String rs = null;
        if (userService.getUserByEmail(email) != null) {
            rs = "yes";
        } else {
            rs = "no";
        }
        return rs;
    }

    @RequestMapping("/checkRedis/{id}")
    public boolean checkRedis(@PathVariable String id) {
        return userService.checkRedis(Integer.valueOf(id));
    }

}
